package com.qf.servlet;

import com.qf.entity.Emp;
import com.qf.service.EmpService;
import com.qf.service.impl.EmpServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @author zdl
 * @date 2025/1/16
 */
@WebServlet("/insert")
public class InsertEmpServlet extends HttpServlet {
    private EmpService empService = new EmpServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        req.setCharacterEncoding("utf-8");
//        resp.setContentType("text/html;charset=utf-8");
        String name = req.getParameter("name");
        String age = req.getParameter("age");
        String sex = req.getParameter("sex");
        String salary = req.getParameter("salary");
        Emp emp = new Emp(0, name, Integer.parseInt(age), sex, Double.parseDouble(salary));
        int i = empService.insertEmp(emp);
        if (i > 0){
            resp.sendRedirect("/list");
        }else {
            PrintWriter out = resp.getWriter();
            out.write("<h2>数据有误！！！</h2>");
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
